package com.floreantpos.ui.config;

import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.floreantpos.model.POSConfig;
import com.floreantpos.model.dao.POSConfigDAO;
import com.floreantpos.swing.FixedLengthTextField;

public class POSConfigurationView extends ConfigurationView{
	
	private POSConfig posConfig;
	private POSConfigDAO dao;
	private JTextField tfDeli = new FixedLengthTextField(20);
	private JTextField tflogo = new FixedLengthTextField(20);
	
	public POSConfigurationView(){
		setLayout(new MigLayout("align 50% 50%"));
		addRow(com.floreantpos.POSConstants.DELI_MODE, tfDeli, "w 300");
		addRow(com.floreantpos.POSConstants.LOGO, tflogo, "w 300");
	}

	@Override
	public boolean save() throws Exception {
		if(!isInitialized()) {
			return true;
		}
		
		String deli;
		String logo;
		
		deli = tfDeli.getText();
		logo = tflogo.getText();
		
		posConfig.setDeli(deli);
		posConfig.setLogo(logo);
		
		dao.saveOrUpdate(posConfig);
		
		return true;
	}

	@Override
	public void initialize() throws Exception {
		dao = new POSConfigDAO();
		posConfig = dao.get(Integer.valueOf(1));
		tfDeli.setText(posConfig.getDeli());
		tflogo.setText(posConfig.getLogo());
		setInitialized(true);

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return com.floreantpos.POSConstants.POS_CONFIG;
	}
}
